#include <stdio.h>
//#include <stdlib.h>
#include <string.h> /*memset()q*/
#include <sys/types.h>
#include <sys/socket.h> /*not POSIX required but important for portability (archaic BSD?)*/
#include <unistd.h> /*close()*/

#include <netinet/in.h>
#include <arpa/inet.h>

#include <netdb.h> /*gethostbyname()*/

#define CONNECT_TO_SOCKET 2501
#define CONNECTION_QUEUE_SIZE 3
#define RECVABLE_SIZE 128


int socketDescriptor,connectedSocketDescriptor;

void sendWrap(const char *text)
{
    send(connectedSocketDescriptor,text,strlen(text),0);
    //printf("%s,%s,%s","S: ",text,"\n");
}

char *recvWrap(void)
{
    char recvdMessage[RECVABLE_SIZE];
    memset(&recvdMessage,0,sizeof(recvdMessage));
    char *recvEnd;

    while(strchr(recvdMessage,'\n')==NULL)
        recv(connectedSocketDescriptor,recvdMessage,RECVABLE_SIZE-1,0);
    recvEnd = strchr(recvdMessage,'\n');
    recvdMessage[recvEnd[0]+1] = '\0';

    return recvdMessage;
}

int main(void)
{
    int yes=1;
    struct sockaddr_in serverAddress;
    socklen_t sin_size;
    //struct hostent *clientAddress;
    struct sockaddr_storage clientAddress;
    const char *clientDomain = "aspmx.l.google.com";
    const char *purpose = "This application sends email to the address specified by the user.\n";
    char sentinel = 'a';

    int len;
    char buf[BUFSIZ+1];
    const char *message = "Hello network.\n\r";
    char recvdMessage[RECVABLE_SIZE];
    char *sender = "mdunmire@mail.usf.edu";
    char *recipient = "mdunmire@mail.usf.edu";
    char *msgBody = "From: ""Bob Example"" <bob@example.org>\r\nTo: ""Alice Example"" <alice@example.com>\r\nDate: Tue, 15 Jan 2008 16:02:43 -0500\r\nSubject: Test message\r\nHello Alice.\r\nThis is a test message with 5 header fields and 4 lines in the message body.\r\nYour friend,\r\nBob";

    char s[INET6_ADDRSTRLEN];

    printf("%s",purpose);


    memset(&serverAddress,0,sizeof(struct sockaddr_in));

    //memset(&clientAddress,0,sizeof(struct sockaddr_in));




    /* type of socket created in socket() */
    serverAddress.sin_family = AF_INET;
    serverAddress.sin_addr.s_addr = INADDR_ANY; //bind on any available physical link
    //inet_pton(AF_INET, "10.0.0.1", &ip4addr.sin_addr); //if we know our IP address

    /* 25 is the port to use for connections */
    serverAddress.sin_port = htons(CONNECT_TO_SOCKET);

    socketDescriptor=socket(AF_INET,SOCK_STREAM,0);
    //if (socketDescriptor!=-1)
    perror("Create socket");

    /* bind the socket to the port specified above */
    bind(socketDescriptor,(struct sockaddr *)&serverAddress,sizeof(serverAddress));

    setsockopt(socketDescriptor,SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
    perror("setsockopt");

//connects to the remote SMTP recipient, rmember clientAddress definition
//    connect(socketDescriptor,(struct sockaddr *) &serverAddress,sizeof (struct sockaddr_in));
//    perror("connecting stream socket");
//    clientAddress = gethostbyname(clientDomain);
//    printf("%s","Still alive\n");
//    memcpy((char *) &serverAddress.sin_addr, (char *) clientAddress->h_addr, clientAddress->h_length);
//    printf("%s","Still alive\n");
//end connect

//listens for a remote connection
    listen(socketDescriptor,CONNECTION_QUEUE_SIZE);

    //while(1) {  // main accept() loop
    sin_size = sizeof(clientAddress);
    connectedSocketDescriptor = accept(socketDescriptor, (struct sockaddr *)&clientAddress, &sin_size);
    //if (connectedSocketDescriptor == -1) {
    perror("accept");
    //continue;
    //}
//end listen

    sendWrap("HELO mail.usf.edu\r\n");
    strcpy(recvdMessage,recvWrap());
    printf("%s",recvdMessage);
    sendWrap("MAIL FROM:<");
    sendWrap(sender);
    sendWrap(">\r\n");
    strcpy(recvdMessage,recvWrap());
    printf("%s",recvdMessage);
    sendWrap("RCPT TO:<");
    sendWrap(recipient);
    sendWrap(">\r\n");
    strcpy(recvdMessage,recvWrap());
    printf("%s",recvdMessage);
    sendWrap("DATA\r\n");

    sendWrap(msgBody);

    sendWrap("\r\n.\r\n");
    strcpy(recvdMessage,recvWrap());
    printf("%s",recvdMessage);
    sendWrap("QUIT\r\n");
    strcpy(recvdMessage,recvWrap());
    printf("%s",recvdMessage);
    //recvdMessage[RECVABLE_SIZE] = '\0';
//    write(1,buf,len);
//    printf("%s","Write\n");

    while(sentinel != '\n')
    {
        printf("%s","Press Enter/Return to exit the program.\n");
        sentinel = getchar();
    }

    close(socketDescriptor);

    return 0;
}
